package logic

import (
	"api/internal/svc"
	"api/internal/types"
	"api/service"
	"context"
	"fmt"
	"github.com/zeromicro/go-zero/core/logx"
)

type OrderFoodGoodsListLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewOrderFoodGoodsListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *OrderFoodGoodsListLogic {
	return &OrderFoodGoodsListLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *OrderFoodGoodsListLogic) OrderFoodGoodsList(req *types.RequestOrderFoodGoodsList) (resp *types.ResponseOrderFoodGoodsList, err error) {

	busService := service.NewBusinessService(l.ctx, l.svcCtx)
	bus := busService.GetLoginBusinessInfo()

	redis := l.svcCtx.RedisPool.Get()
	defer redis.Close()

	goodsService := service.NewOrderFoodService(l.ctx, l.svcCtx)

	list, err := goodsService.GetGoodsList(redis, bus.Id, req.CategoryId)

	fmt.Println(list)

	resp = &types.ResponseOrderFoodGoodsList{}

	if list != nil {
		arr := make([]types.OrderFoodGoods, len(*list))

		for index, item := range *list {
			arr[index].Id = item.Id
			arr[index].Title = item.Title
			arr[index].Price = item.Price
			arr[index].Img = item.Img
			arr[index].Specifications = item.Specifications
			arr[index].Properties = item.Properties
			arr[index].Jialiao = item.Jialiao
			arr[index].Des = item.Des

		}

		resp.List = arr
	}

	resp.Code = 200
	return resp, nil

}
